home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Tutorial / Cookbook / 16.open_file / notes < prev    next >
Text File  |  1995-06-12  |  4KB  |  145 lines

  1. Objective:
  2. Demonstrate use of the OpenPanel object.
  3.  
  4. Summary:  Create a custom object which will open a file using the OpenPanel class. This class will bring up the open panel which will return a pathname to
  5. the file and call the "openFile:" method.  The sample code in openFile
  6. will read a file of integers and strings from a disk file into arrays.
  7.  
  8. Terms:
  9.  
  10. Dependancies: hello
  11.  
  12. Discussion: We start by creating an instance of the OpenPanel in the
  13. "new" method. The method "openRequest" is the method that gets executed
  14. after the "Open..." menu is selected.  It sends the message:
  15.  
  16.    runModalForTypes:
  17.  
  18. to the open panel object.  It then gets the pathname to the file returned
  19. by sending the "filename" message to the open panel.  This filename is
  20. then passed on to a call to the "openFile:" method.  The openFile method
  21. is just a wrapper for some generic C code that reads a file of numbers
  22. and strings.
  23.    
  24. Method:
  25.  
  26. 1) Create a subclass of Object called "MyObject" with a single openRequset:
  27. action and an OpenReq outlet.
  28.  
  29. 2) Create an instance of this object.
  30.  
  31. 3) Add a Window submenu and add an "Open..." submenu to that.
  32.  
  33. 4) Connect the Open menu to an instance of MyObject
  34.  
  35. 5) Add the following code to MyObject.h and MyObject.m
  36. ----------------------MyObject.h-----------------------------------
  37. #import <appkit/appkit.h>
  38.  
  39. #define MAX_CHARS_PER_LINE 100
  40. #define MAX_LINES 100
  41.  
  42. @interface MyObject:Object
  43. {
  44.     id    openReq;    // OpenPanel for open requests 
  45. }
  46.  
  47. - openRequest:sender;
  48. -(int) openFile:(const char *)fileName;
  49.  
  50. @end
  51. ------------------MyObject.m---------------------------------------
  52. #import "MyObject.h"
  53.  
  54. @implementation MyObject
  55.  
  56. #import "MyObject.h"
  57.  
  58. @implementation MyObject
  59.  
  60. + new
  61. {
  62.   self = [super new];
  63.   [NXApp setDelegate:self];
  64.   openReq = [OpenPanel new];
  65.   return self;
  66. }
  67.  
  68. - showError: (char *)errorMessage
  69. {
  70.   NXAlert(errorMessage,"OK",NULL,NULL);
  71. }
  72.  
  73. // openRequest: opens a new file. It puts up a open panel, and, if the user
  74. // doesn't cancel, it reads the specified archive file. If the selected file
  75. // is not a proper archive file, then openRequest: will complain.
  76.  
  77. - openRequest:sender
  78. {
  79.     const char *fileName;
  80.     const char *const types[4] = {"data",NULL};
  81.     int ok;
  82.  
  83.     if ([openReq runModalForTypes:types] && (fileName =[openReq filename])) {
  84.     [self openFile:fileName];
  85.     }
  86.     else
  87.     [self showError:"No file chosen or could not open file"];
  88.     return self;
  89. }
  90.  
  91. -(int) openFile:(const char *)fileName
  92. {
  93.   int i=0;
  94.   char line[MAX_CHARS_PER_LINE];
  95.   char string[MAX_CHARS_PER_LINE][MAX_LINES];
  96.   int value[MAX_LINES];
  97.   FILE *input_fp;
  98.   printf("Opening File = %s\n", fileName);
  99.   
  100.   if (( input_fp = fopen(fileName, "r") ) == NULL ) {
  101.      [self showError:"Could not open file"];
  102.      fprintf(stderr, "File: %s Could not be opened.\n", fileName);
  103.      return NO;
  104.   }
  105.   
  106.   while (fgets(line, MAX_CHARS_PER_LINE, input_fp) != NULL)
  107.   {
  108.      i++;
  109.      if (i >= MAX_LINES) break;
  110.      sscanf(line, "%d %s", &value[i], string[i]);
  111.      printf("  value = %d label = %s\n", value[i], string[i]);
  112.   }
  113.   printf("%d data points read\n", i);
  114.   fclose(input_fp);
  115.   return YES;
  116.  
  117. }
  118. ------------------------------------------------------------------
  119. 6) Create a file "a.data" Which has the following data:
  120. -------------------fruit.data---------------
  121. 10 apples
  122. 20 oranges
  123. 30 grapes
  124. 40 pears
  125. 50 bananas
  126. ---------------------------------------
  127. 7) Type "make" in a shell and select "open" and the file fruit.data.
  128. You should see the following in the shell window:
  129.  
  130. $ open
  131. Opening File = /mccreary/InDevelopment/open_file/fruit.data
  132.   value = 10 label = apples
  133.   value = 20 label = oranges
  134.   value = 30 label = grapes
  135.   value = 40 label = pears
  136.   value = 50 label = bananas
  137. 5 data points read
  138.  
  139.  
  140. Further Questions:
  141.  
  142.  
  143.    
  144.  
  145.